package com.spzx.product.controller;

import com.spzx.common.core.utils.ServletUtils;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.annotation.RequiresPermissions;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.IProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.List;

/**
 * <p>
 * 商品单位 前端控制器
 * </p>
 *
 * @author zlr
 * @since 2024-11-14
 */
@Tag(name="商品单位")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private IProductUnitService productUnitService;
    @RequiresPermissions("base:productUnit:list")
    @Operation(summary = "获取商品单位列表")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(description = "商品单位名称")String name){
        //分页
        startPage();
        List<ProductUnit> list= productUnitService.selectProductUnitList(name);
        return  getDataTable(list);
    }

    //获取商品单位详细信息
    @RequiresPermissions("base:productUnit:query")
    @Operation(summary ="获取商品单位详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@Parameter(description = "商品单位id")@PathVariable("id") Long id){
        return  success(productUnitService.getById(id));
    }

    //新增商品单位
    @RequiresPermissions("base:productUnit:add")
    @Operation(summary = "新增商品单位")
    @PostMapping
    public AjaxResult add(@Parameter(description = "商品单位") @RequestBody ProductUnit productUnit){
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.save(productUnit));
    }
    //修改商品单位
    @RequiresPermissions("base:productUnit:edit")
    @Operation(description = "修改商品单位")
    @PutMapping
    public AjaxResult edit(@Parameter(description = "商品单位") @RequestBody ProductUnit productUnit){
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productUnitService.updateById(productUnit));
    }

    //删除商品单位
    @RequiresPermissions("base:productUnit:remove")
    @Operation(description = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @Parameter(description = "商品单位id列表")
            @PathVariable List<Long> ids
    ){
        return toAjax(productUnitService.removeByIds(ids));
    }


}
